package com.spdg.ring.bo;

import android.app.Activity;
import android.os.AsyncTask.Status;
import cn.wolf.base.BaseBo;
import cn.wolf.http.HttpCallBack;

import com.spdg.ring.common.Config;
import com.spdg.ring.datamgr.MenuDataMgr;
import com.spdg.ring.entity.MenuEntity;
import com.spdg.ring.resp.MenuResp;
import com.spdg.ring.task.MenuTask;

/**
 * <p>Title:  菜单配置业务逻辑层</p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-20             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuBo extends BaseBo implements HttpCallBack<MenuResp> {

	private MenuTask task;
	private HttpCallBack<MenuResp> iCall;

	public MenuBo(Activity activity, HttpCallBack<MenuResp> call) {
		super(activity);
		this.iCall = call;
	}

	public void request() {
		task = new MenuTask(mActivity, this, Config.URL.URL_MENU_CONFIG);
		task.execute();
	}

	public boolean isRunning() {
		if (task == null || !task.getStatus().equals(Status.RUNNING)) {
			return false;
		}
		return true;
	}

	public void cancle() {
		if (task != null) {
			task.cancel(true);
		}
	}

	@Override
	public void call(MenuResp response) {

		if (response.isSuccess()) {
			MenuEntity entity = response.getMenu(); // 保存数据到缓存
			MenuDataMgr.getInstance().refresh(mActivity, entity);

			if (entity != null) // 保存数据到数据库
				MenuDataMgr.getInstance().saveDataSqlite(mActivity, entity.getMenus());
		}

		if (iCall != null) {
			iCall.call(response);
		}

	}

	@Override
	public void progress(Object... obj) {
		if (iCall != null) {
			iCall.progress(obj);
		}
	}
	
	@Override
	public void onNetWorkError() {
		if (iCall != null) {
			iCall.onNetWorkError();
		}
	}

}
